package com.gozap.log4j;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;

public class TestLog {

	@Test
	public void log() throws Exception {
		Logger logger = Logger.getLogger("com.foo");
		logger.addAppender(new ConsoleAppender(new PatternLayout("%r [%t] %-5p %c - %m%n")));
		logger.setLevel(Level.DEBUG);
		logger.warn("Low fuel level.");
		logger.debug("Starting search for nearest gas station.");

		Logger barlogger = Logger.getLogger("com.foo.Bar");
		barlogger.addAppender(new FileAppender(new PatternLayout("%r %-5p %c - %m%n"), "aa.log"));
		barlogger.setAdditivity(false);
		barlogger.info("Located nearest gas station.");

		barlogger.debug("Exiting gas station search");
	}

	// Define a static logger variable so that it references the
	// Logger instance named "TestLog".
	static Logger logger = Logger.getLogger(TestLog.class);

	@Test
	public void baseConfig() {
		// Set up a simple configuration that logs on the console.
		BasicConfigurator.configure();

		logger.info("Entering application.");
		Bar bar = new Bar();
		bar.doIt();
		logger.info("Exiting application.");
	}

	@Test
	public void propertyConfig() {
		Logger logger = Logger.getLogger("com.*");

		// BasicConfigurator replaced with PropertyConfigurator.
		PropertyConfigurator.configure("src/main/java/com/gozap/log4j/log4j.properties");

		logger.info("property Entering application.");
		Bar bar = new Bar();
		bar.doIt();
		logger.info("property Exiting application.");
	}
}
